<?php

namespace App\View\Components;

use App\Models\Warranty;
use App\Models\WarrantyVerify;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\View\Component;

class WarrantyMessage extends Component
{
    public $created_by = '';
    public $created_at = '';
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct(Request $request, Warranty $warranty)
    {
        $this->warranty = $warranty;
        $this->warranty_id = $warranty->id;
        $this->msg = [];
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|string
     */
    public function render()
    {
        $message = $this->getMessage();
        // dd($message);

        return view('components.warranty-message', [
            'message' => $message,
        ]);
    }

    public function getMessage()
    {
        if ($this->warranty->approved == 2) {
            $model = WarrantyVerify::where('warranty_id', $this->warranty_id)
                ->orderByDesc('id')
                ->first();
            $this->created_at = $model->updated_at;
            $this->created_by = getJc($model->updated_by);
            return [$model->note];
        }

        if (empty($this->warranty_id)) {
            return [];
        } else {
            $model = DB::table('warranty_msg')
                ->where('warranty_id', $this->warranty_id)
                ->whereNull('deleted_at')
                ->orderByDesc('id')
                ->first();
            if (empty($model) || $model->status != 0) {
                return [];
            } else {
                $content = $model->content;
                $this->created_at = $model->created_at;
                // $this->created_by = $model->created_by;
                $this->created_by = getJc($model->created_by);
                // dd($content);
                return json_decode($content, 1);
            }

        }

    }


}
